<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/14/014
 * Time: 15:08
 */
namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Http\Common\Lib\Time;
use App\Models\AdminUser;
use App\Models\Match;
use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use QL\QueryList;

class IndexController extends BaseAdminController
{
    public function index(){
        $userInfo = session('adminUser');
        return view('admin.index.index1',compact('userInfo'));
    }

    public function home(){
        //$userModel = new AdminUser();
        //dd($userModel->getTable());
        /*$res = Common::ipToAddress(\request()->getClientIp());
        dd($res);*/
        /*Redis::set('name','test');
        dd(Redis::get('name'));*/
       /* $date = '2019-01-09';
        $this->getSessionScore();
        $this->test($date);*/
        //$matches = Match::updateMatches();
	    /*$time = time();
	    $info = Time::nextMonthDay($time);
	    if(is_array($info)){
	    	foreach ($info as $key=>$value){
	    		$info[$key] =date('Y-m-d H:i:s',$value);
		    }
	    }else{
	    	$info = date('Y-m-d H:i:s',$info);
	    }
	    dd($info);*/
        return view('admin.index.home');
    }

    /**
     * 获取左侧菜单
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenu(){
        /*$menu = [
            [
                'id'=>10,'title'=>'系统管理','subMenus'=>[
                    ['id'=>11,'title'=>'管理员列表','url'=>'admin/admin'],
                    ['id'=>12,'title'=>'管理员角色','url'=>'admin/role'],
                    ['id'=>13,'title'=>'菜单管理','url'=>'admin/menu']
                ]
            ],
          ['id'=>1,'title'=>'页面','url'=>'','subMenus'=>[
               [ 'id'=>2,'title'=>'登陆','url'=>'admin/login'],
              ['id'=>10,'title'=>'文章标签','url'=>'admin/tag1'],
            ]
          ],
          ['id'=>3,'title'=>'集成组件','url'=>'','subMenus'=>[
              ['id'=>4,'title'=>'树状表格','url'=>'treeTable'],
              ['id'=>5,'title'=>'权限控制树','url'=>'treeAuth','subMenus'=>[
                    ['id'=>6,'title'=>'骗你的呀','url'=>'jiujiu'],
                ]
              ],
          ]],
            ['id'=>7,'title'=>'文章管理','subMenus'=>[
                ['id'=>8,'title'=>'文章列表','url'=>'admin/post'],
                ['id'=>9,'title'=>'文章标签','url'=>'admin/tag'],
            ]]
        ];*/
        //$data = Common::dataFormat(200,'success',$menu);
        //dump($menu);
        //return response()->json(['code'=>200,'msg'=>'success','data'=>$menu]);
        $userInfo = session('adminUser');
        $menuModel = new Menu();
        if($userInfo->id == 1){
            $menus = $menuModel->getSideMenus(0,true,true);
        }else{
            $roles = $userInfo->roles;
            $menuIds = [];
            foreach ($roles as $role){
                $ids = $role->menus->pluck('id')->toArray();
                $menuIds = array_merge($menuIds,$ids);
            }
            $menuIds = array_unique($menuIds);
            $menus = $menuModel->getSideMenus(0,true,false,$menuIds);
        }
        return response()->json(Common::dataFormat(200,'success',$menus));
        //return Common::jsonFormat(200,'success',$menu);
    }

    function test($date=null){
        $domain = 'http://trade.500.com/jczq/';
        if(!$date){
            $date = date('Y-m-d');
        }
        $ql = QueryList::get($domain,['date'=>$date]);
       $range = ".bet-main tr.bet-tb-tr[data-processdate='{$date}']";
       $rules = [
           'num'=>['td.td-no','text'],
           'league'=>['td.td-evt>a','title'],
           'endTime'=>['td.td-endtime','text'],
           'host'=>['td.td-team .team-l a','text'],
           'guest'=>['td.td-team .team-r a','text'],
           'balls'=>['td.td-rang .itm-rangA2','text','-.ico-dg'],
           'sp11'=>['td.td-betbtn .itm-rangB1 p[data-value=\'3\']','data-sp'],
           'sp12'=>['td.td-betbtn .itm-rangB1 p[data-value=\'1\']','data-sp'],
           'sp13'=>['td.td-betbtn .itm-rangB1 p[data-value=\'0\']','data-sp'],
           'sp21'=>['td.td-betbtn .itm-rangB2 p[data-value=\'3\']','data-sp'],
           'sp22'=>['td.td-betbtn .itm-rangB2 p[data-value=\'1\']','data-sp'],
           'sp23'=>['td.td-betbtn .itm-rangB2 p[data-value=\'0\']','data-sp'],
       ];
        $data = $ql->get($domain,['date'=>$date])->range($range)->rules($rules)->encoding('UTF-8','GB2312')->removeHead()->queryData();
        $dateTime = strtotime($date);
        $year = date('Y',$dateTime);
        $session = date('Ymd',$dateTime);
        foreach ($data as $key=>$value){
            $data[$key]['session'] = $session;
            $data[$key]['num'] = substr($value['num'],-3);
            $data[$key]['endTime'] = $year.'-'.$value['endTime'];
            if(!$value['sp11']){
                $data[$key]['sp11'] = $data[$key]['sp12'] = $data[$key]['sp13'] = 1;
            }
        }
        dd($data);

        /*$tables = $ql->find('.bet-main table.bet-table')->map(function ($table){
            return $table->find('tr')->all();
        });
        dd($tables);*/
    }

    function getSessionScore($session=null){
        if(!$session){
            $session = date('Ymd',strtotime('-1 day'));
        }
        $time = strtotime($session);
        $date = date('Y-m-d',$time);
        $domain = 'http://trade.500.com/jczq/';
        $ql = QueryList::get($domain,['date'=>$date]);
        $range = ".bet-main tr.bet-tb-tr[data-processdate='{$date}']";
        $rules = [
            'num'=>['td.td-no','text'],
            'league'=>['td.td-evt>a','title'],
            'endTime'=>['td.td-endtime','text'],
            'host'=>['td.td-team .team-l a','text'],
            'guest'=>['td.td-team .team-r a','text'],
            'balls'=>['td.td-rang .itm-rangA2','text','-.ico-dg'],
            'score'=>['td.td-team .score','text']
        ];
        $data = $ql->get($domain,['date'=>$date])->range($range)->rules($rules)->encoding('UTF-8','GB2312')->removeHead()->queryData();
        foreach ($data as $key=>$value){
            $data[$key]['session'] = $session;
            $data[$key]['num'] = substr($value['num'],-3);
        }
        dd($data);

    }



}